//
//  PersonalModel.swift
//  TrafficViolation
//
//  Created by eaglelive on 2017/4/25.
//  Copyright © 2017年 eaglelive. All rights reserved.
//

import Foundation
import SwiftyJSON
class PersonalModel: NSObject {
    var userID:String = ""
    var loginID:String = ""
    var realName:String = ""
    var icon:String = ""
    var nickName:String = ""
    var phoneNum:String = ""
    var phoneMac:String = ""
    var ustatus:String = ""
    var sex:String = ""
    var userType:String = ""
    var upasswd:String = ""
    var tokenID:String = ""
    //初始化
    init(dict : JSON,tokenID:String) {
        super.init()
        self.userID = dict["userID"].stringValue
        self.loginID = dict["loginID"].stringValue
        self.realName = dict["realName"].stringValue
        self.icon = dict["icon"].stringValue
        self.nickName = dict["nickName"].stringValue
        self.phoneNum = dict["phoneNum"].stringValue
        self.phoneMac = dict["phoneMac"].stringValue
        self.ustatus = dict["ustatus"].stringValue
        self.sex = dict["sex"].stringValue
        self.userType = dict["userType"].stringValue
        self.upasswd = dict["upasswd"].stringValue
        self.tokenID = tokenID
    }
    //将自身插入数据库接口
    func insertOrUpdatePersonalToDB() -> Bool {
        let count = SQLiteManager.shareInstance().queryDBData(querySQL: "SELECT userID FROM 'user_t_consumer' where userID ='\(userID)'")?.count
        var inserOrUpdateSql = ""
        if(nil == count || 0 == count){
            inserOrUpdateSql = "INSERT INTO 'user_t_consumer' (userID,phoneNum,loginID,upasswd,icon,nickName,realName,tokenID,phoneMac,ustatus,userType,upasswd) VALUES ('\(userID)','\(phoneNum)','\(loginID)','\(upasswd)','\(icon)','\(nickName)','\(realName)','\(tokenID)','\(phoneMac)','\(ustatus)','\(userType)','\(upasswd)');"
        }else{
            inserOrUpdateSql = "UPDATE 'user_t_consumer' SET userID='\(userID)',phoneNum='\(phoneNum)',loginID='\(loginID)', upasswd='\(upasswd)', icon='\(icon)',nickName='\(nickName)',realName='\(realName)',tokenID='\(tokenID)',phoneMac='\(phoneMac)',ustatus='\(ustatus)',userType='\(userType)',upasswd='\(upasswd)' WHERE userID='\(userID)'"
        }
        print("inserOrUpdateSql:\(inserOrUpdateSql)")
        if SQLiteManager.shareInstance().execSQL(SQL: inserOrUpdateSql) {
            return true
        }
        return false
    }
    //查询用户基本
    class func loadPersonalFromDB(id:String) -> PersonalModel? {
        let querySQL = "SELECT userID,phoneNum,loginID,icon,nickName,realName,tokenID FROM 'user_t_consumer' where userID = '\(id)'"
        print("querySQL:\(querySQL)")
        //取出数据库中用户表所有数据
        let personalArr = SQLiteManager.shareInstance().queryDBData(querySQL: querySQL)
        //将字典数组转化为模型数组
        if let tempPersonalDictM = personalArr {
            // 判断数组如果有值,则遍历,并且转成模型对象,放入另外一个数组中
            var personalModelArrM = [PersonalModel]()
            for dict in tempPersonalDictM {
                personalModelArrM.append(PersonalModel(dict: JSON(dict), tokenID: dict["tokenID"] as! String))
            }
            if personalModelArrM.count > 0 {
                return personalModelArrM[0]
            }
        }

        return nil
    }
    //删除用户信息
    class func deletePersonal(userID:String)-> Bool{
        let deleteSQL = "DELETE FROM user_t_consumer WHERE userID = '\(userID)'"
        print("deleteSQL:\(deleteSQL)")
        if SQLiteManager.shareInstance().execSQL(SQL: deleteSQL) {
            return true
        }
        return false
    }
    //修改图像
    class func changeIcon(newIcon:String,userID:String) -> Bool {
        let changeIconSQL = "UPDATE 'user_t_consumer' SET icon='\(newIcon)' WHERE userID='\(userID)'"
        if SQLiteManager.shareInstance().execSQL(SQL: changeIconSQL) {
            return true
        }
        return false
    }
    //修改昵称
    class func changeNickName(nickName:String,userID:String)-> Bool{
        let changeIconSQL = "UPDATE 'user_t_consumer' SET nickName='\(nickName)' WHERE userID='\(userID)'"
        if SQLiteManager.shareInstance().execSQL(SQL: changeIconSQL) {
            return true
        }
        return false
    }
}
